echowithcolor(COLOR GREEN "-- Configure libcotask")

# ========== filter sources files ==========
file(
  GLOB_RECURSE COTASK_SRC_LIST
  RELATIVE "${PROJECT_LIBCOTASK_SRC_DIR}"
  ${PROJECT_LIBCOTASK_INC_DIR}/*.h ${PROJECT_LIBCOTASK_INC_DIR}/*.hpp)

file(GLOB_RECURSE SRC_LIST ${PROJECT_LIBCOTASK_SRC_DIR}/*.c ${PROJECT_LIBCOTASK_SRC_DIR}/*.cpp)
list(LENGTH SRC_LIST SRC_LIST_LENGTH)
if(${SRC_LIST_LENGTH} GREATER 0)
  list(APPEND COTASK_SRC_LIST ${SRC_LIST})
endif()

# ========== build library ==========
source_group_by_dir(COTASK_SRC_LIST)

if(BUILD_SHARED_LIBS OR LIBCOPP_USE_DYNAMIC_LIBRARY)
  add_library(${PROJECT_LIBCOTASK_LIB_LINK} SHARED ${COTASK_SRC_LIST})
  set_target_properties(
    ${PROJECT_LIBCOTASK_LIB_LINK}
    PROPERTIES C_VISIBILITY_PRESET "hidden"
               CXX_VISIBILITY_PRESET "hidden"
               VERSION ${LIBCOPP_VERSION}
               SOVERSION ${LIBCOPP_VERSION})
  target_compile_definitions(${PROJECT_LIBCOTASK_LIB_LINK} PRIVATE LIBCOPP_API_COTASK_NATIVE=1 LIBCOPP_API_DLL=1)
  set_target_properties(${PROJECT_LIBCOTASK_LIB_LINK} PROPERTIES INTERFACE_COMPILE_DEFINITIONS LIBCOPP_API_DLL=1)
else()
  add_library(${PROJECT_LIBCOTASK_LIB_LINK} STATIC ${COTASK_SRC_LIST})
  set_target_properties(
    ${PROJECT_LIBCOTASK_LIB_LINK}
    PROPERTIES C_VISIBILITY_PRESET "hidden"
               CXX_VISIBILITY_PRESET "hidden"
               VERSION ${LIBCOPP_VERSION})
  target_compile_definitions(${PROJECT_LIBCOTASK_LIB_LINK} PRIVATE LIBCOPP_API_COTASK_NATIVE=1)
endif()

target_link_libraries(${PROJECT_LIBCOTASK_LIB_LINK} PUBLIC ${PROJECT_LIBCOPP_LIB_LINK})
target_include_directories(${PROJECT_LIBCOPP_LIB_LINK} INTERFACE "$<INSTALL_INTERFACE:include>")

if(LIBCOPP_MACRO_ENABLE_STD_COROUTINE)
  add_target_properties(${PROJECT_LIBCOTASK_LIB_LINK} INTERFACE_COMPILE_FEATURES cxx_std_20)
  if(MSVC)
    add_target_properties(${PROJECT_LIBCOTASK_LIB_LINK} INTERFACE_COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:/await>")
  else()
    string(FIND "${CMAKE_CXX_FLAGS}" "-fcoroutines-ts" LIBCOPP_FIND_COROUTINE_OPTION)
    if(LIBCOPP_FIND_COROUTINE_OPTION GREATER_EQUAL 0)
      add_target_properties(${PROJECT_LIBCOTASK_LIB_LINK} INTERFACE_COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:-fcoroutines-ts>")
    else()
      add_target_properties(${PROJECT_LIBCOTASK_LIB_LINK} INTERFACE_COMPILE_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:-fcoroutines>")
    endif()
  endif()
endif()

install(
  TARGETS ${PROJECT_LIBCOTASK_LIB_LINK}
  EXPORT ${PROJECT_LIBCOTASK_EXPORT_NAME}
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(
  DIRECTORY "${PROJECT_LIBCOTASK_INC_DIR}"
  DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
  USE_SOURCE_PERMISSIONS
  PATTERN ".svn" EXCLUDE
  PATTERN ".git" EXCLUDE)

export(
  TARGETS ${PROJECT_LIBCOTASK_LIB_LINK}
  FILE "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/${PROJECT_LIBCOTASK_EXPORT_NAME}.cmake"
  NAMESPACE libcopp::
  EXPORT_LINK_INTERFACE_LIBRARIES)
install(
  EXPORT ${PROJECT_LIBCOTASK_EXPORT_NAME}
  NAMESPACE "libcopp::"
  DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake")
